package com.xjy.web.controller.system.basics;

import java.util.List;
import java.util.Arrays;

import com.xjy.common.exception.UtilException;
import com.xjy.system.domain.SysTransactionUnit;
import com.xjy.system.domain.vo.SysMaterialMngVo;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.xjy.common.annotation.RepeatSubmit;
import com.xjy.common.annotation.Log;
import com.xjy.common.core.controller.BaseController;
import com.xjy.common.core.domain.AjaxResult;
import com.xjy.common.enums.BusinessType;
import com.xjy.system.domain.vo.SysTransactionUnitVo;
import com.xjy.system.domain.bo.SysTransactionUnitQueryBo;
import com.xjy.system.domain.bo.SysTransactionUnitAddBo;
import com.xjy.system.domain.bo.SysTransactionUnitEditBo;
import com.xjy.system.service.ISysTransactionUnitService;
import com.xjy.common.utils.poi.ExcelUtil;
import com.xjy.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 往来(单位-人)信息Controller
 *
 * @author zc
 * @date 2022-02-09
 */
@Api(value = "往来(单位-人)信息控制器", tags = {"往来(单位-人)信息管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/SysTransactionUnit")
public class SysTransactionUnitController extends BaseController {

    private final ISysTransactionUnitService iSysTransactionUnitService;

    /**
     * 查询往来(单位-人)信息列表
     */
    @ApiOperation("查询往来(单位-人)信息列表")
    @PreAuthorize("@ss.hasPermi('system:SysTransactionUnit:list')")
    @GetMapping("/list")
    public TableDataInfo list(@Validated SysTransactionUnit sysTransactionUnit) {
        return iSysTransactionUnitService.queryPageList(sysTransactionUnit);
    }
	/**
	 * 查询往来(单位-人)信息列表
	 */
	@ApiOperation("查询往来(单位-人)信息列表")
	@PreAuthorize("@ss.hasPermi('system:SysTransactionUnit:list')")
	@GetMapping("/selectList")
	public List<SysTransactionUnitVo> selectList(@Validated SysTransactionUnitQueryBo bo) {
		bo.setAuditStatus(true);
		return iSysTransactionUnitService.queryList(bo);
	}
    /**
     * 导出往来(单位-人)信息列表
     */
    @ApiOperation("导出往来(单位-人)信息列表")
    @PreAuthorize("@ss.hasPermi('system:SysTransactionUnit:export')")
    @Log(title = "往来(单位-人)信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<SysTransactionUnitVo> export(@Validated SysTransactionUnitQueryBo bo) {
        List<SysTransactionUnitVo> list = iSysTransactionUnitService.queryList(bo);
        ExcelUtil<SysTransactionUnitVo> util = new ExcelUtil<SysTransactionUnitVo>(SysTransactionUnitVo.class);
        return util.exportExcel(list, "往来(单位-人)信息");
    }

    /**
     * 获取往来(单位-人)信息详细信息
     */
    @ApiOperation("获取往来(单位-人)信息详细信息")
    @PreAuthorize("@ss.hasPermi('system:SysTransactionUnit:query')")
    @GetMapping("/{id}")
    public AjaxResult<SysTransactionUnitVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iSysTransactionUnitService.queryById(id));
    }

    /**
     * 新增往来(单位-人)信息
     */
    @ApiOperation("新增往来(单位-人)信息")
    @PreAuthorize("@ss.hasPermi('system:SysTransactionUnit:add')")
    @Log(title = "往来(单位-人)信息", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody SysTransactionUnitAddBo bo) {
        return toAjax(iSysTransactionUnitService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改往来(单位-人)信息
     */
    @ApiOperation("修改往来(单位-人)信息")
    @PreAuthorize("@ss.hasPermi('system:SysTransactionUnit:edit')")
    @Log(title = "往来(单位-人)信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping("/edit")
    public AjaxResult<Void> edit(@Validated @RequestBody SysTransactionUnitEditBo bo) {
		SysTransactionUnitVo sysTransactionUnitVo = iSysTransactionUnitService.queryById(bo.getId());
		if(sysTransactionUnitVo.getAuditStatus()){
			throw new UtilException("该数据已经审核无法修改！");
		}
        return toAjax(iSysTransactionUnitService.updateByEditBo(bo) ? 1 : 0);
    }

	/**
	 * 审核往来(单位-人)信息
	 */
	@ApiOperation("修改往来(单位-人)信息")
	@PreAuthorize("@ss.hasPermi('system:SysTransactionUnit:edit')")
	@Log(title = "往来(单位-人)信息", businessType = BusinessType.UPDATE)
	@RepeatSubmit
	@PutMapping("/audit")
	public AjaxResult<Void> audit(@Validated @RequestBody SysTransactionUnitEditBo bo) {
		SysTransactionUnitVo sysTransactionUnitVo = iSysTransactionUnitService.queryById(bo.getId());
		if(sysTransactionUnitVo.getAuditStatus()){
			throw new UtilException("该数据无需重新审批！");
		}
		bo.setAuditStatus(true);
		return toAjax(iSysTransactionUnitService.updateByEditBo(bo) ? 1 : 0);
	}

    /**
     * 删除往来(单位-人)信息
     */
    @ApiOperation("删除往来(单位-人)信息")
    @PreAuthorize("@ss.hasPermi('system:SysTransactionUnit:remove')")
    @Log(title = "往来(单位-人)信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iSysTransactionUnitService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
